﻿// ===============================================================================
// DotNet.Platform 开发框架 2016 版权所有
// ===============================================================================

using System.Collections.Generic;
using System.Linq;
using DotNet.Collections;
using DotNet.Extensions;
using DotNet.Utility;
using DotNet.Helper;
using System;
using DotNet.Core.Entity;
using DotNet.Data;
using DotNet.Data.Extensions;

namespace DotNet.Core.Service
{
    /// <summary>
    /// 文章信息服务
    /// </summary>
    public class ArticleService
    {
        private CoreRepository<Article> repos = new CoreRepository<Article>();
        private static readonly Cache<string, Article> Cache = new Cache<string, Article>();

        /// <summary>
        /// 保存文章
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="isCreate">是否新增</param>
        public BoolMessage Save(Article entity, bool isCreate)
        {
            var result = EntityHelper.ValidAsMessage(entity, "<br>");
            if (result.Failure)
            {
                return result;
            }

            if (isCreate)
            {
                entity.CreateTime = DateTime.Now;
                repos.Insert(entity);
            }
            else
            {
                entity.ModifyTime = DateTime.Now;
                repos.UpdateExclude(entity, p => p.CreateTime);
            }
            Cache.Set(entity.Id, entity);
            return BoolMessage.True;
        }

        /// <summary>
        /// 设置文章状态
        /// 状态 0待发送 1已发送
        /// </summary>
        /// <param name="id">文章状态主键</param>
        /// <param name="status">状态 0待发送 1已发送</param>
        public BoolMessage SetStatus(string id, bool status)
        {
            var entity = new Article();
            entity.Status = status;
            repos.UpdateInclude(entity, p => p.Id == id, p => p.Status);
            return BoolMessage.True;
        }

        /// <summary>
        /// 删除文章
        /// </summary>
        /// <param name="ids">主键数组</param>
        public BoolMessage Delete(string[] ids)
        {
            repos.Delete(ids);
            Cache.Remove(ids);
            return BoolMessage.True;
        }

        /// <summary>
        /// 获取文章
        /// </summary>
        /// <param name="id">主键</param>
        public Article Get(string id)
        {
            var entity = Cache.Get(id, () =>
            {
                return repos.Get(p => p.Id == id);
            });
            return entity;
        }

        /// <summary>
        /// 获取模块文章列表
        /// </summary>
        /// <param name="categoryId">分类Id</param>
        /// <param name="len">条数</param>
        public List<Article> GetModuleList(string categoryId, int len)
        {
            var query = repos.SQL;
            if (categoryId.IsNotEmpty())
            {
                var categoryids = ArticleCategoryService.Instance.
                    GetChildNodeList(categoryId, true).Select(p => p.Id).ToArray();
                query.Where(p => p.CategoryId.In(categoryids));
            }
            query.OrderByDesc(p => p.CreateTime);
            query.ExcludeSelect(p => p.Contents);
            return repos.Query(query).Take(len).ToList();
        }

        /// <summary>
        /// 获取文章列表
        /// </summary>
        public List<Article> GetSlideList()
        {
            var query = repos.SQL;
            query.Where(p => p.IsSlide == true && p.Status == true);
            return repos.Query(query).ToList();
        }


        /// <summary>
        /// 获取文章列表
        /// </summary>
        public List<Article> GetList(string key, string categoryId, bool? status,
            DateTime? startDate, DateTime? endDate)
        {
            var query = repos.SQL;
            SetQuery(query, key, categoryId, status, startDate, endDate);
            return repos.Query(query).ToList();
        }


        /// <summary>
        /// 获取新闻分页列表
        /// </summary>
        /// <param name="pageCondition">分页对象</param>
        /// <param name="name">名称关键字</param>
        public PageList<Article> GetPageList(PaginationCondition pageCondition, string key,
            string categoryId, bool? status, DateTime? startDate, DateTime? endDate)
        {
            pageCondition.SetDefaultOrder(nameof(Article.CreateTime));
            var query = repos.PageQuery(pageCondition);
            SetQuery(query, key, categoryId, status, startDate, endDate);
            query.ExcludeSelect(p => p.Contents, p => p.PhotoUrl);
            return repos.Page(query);
        }

        /// <summary>
        /// 设置查询条件
        /// </summary>
        /// <param name="query">查询对象</param>
        /// <param name="search">数据对象</param>
        public void SetQuery(SQLQuery<Article> query, string key,
            string categoryId, bool? status, DateTime? startDate, DateTime? endDate)
        {
            if (key.IsNotEmpty())
            {
                key = key.Trim();
                query.Where(p => p.Subject.Contains(key));
            }
            if (categoryId.IsNotEmpty())
            {
                var categoryids = ArticleCategoryService.Instance.
                    GetChildNodeList(categoryId, true).Select(p => p.Id).ToArray();
                query.Where(p => p.CategoryId.In(categoryids));
            }
            if (status.HasValue)
            {
                query.Where(p => p.Status == status.Value);
            }
            if (startDate.HasValue)//开始日期
            {
                query.Where(p => p.CreateTime >= startDate.Value);
            }
            if (endDate.HasValue)//结束日期
            {
                query.Where(p => p.CreateTime <= endDate.Value.AddDays(1));
            }
        }

        /// <summary>
        /// 获取新闻分页列表
        /// </summary>
        /// <param name="pageCondition">分页对象</param>
        /// <param name="name">名称关键字</param>
        public PageList<Article> GetSitePageList(PaginationCondition pageCondition,
        string categoryId, string key = null)
        {
            pageCondition.SetDefaultOrder(nameof(Article.CreateTime));
            var query = repos.PageQuery(pageCondition);
            query.Where(p => p.Status == true);
            if (key.IsNotEmpty())
            {
                key = key.Trim();
                query.Where(p => p.Subject.Contains(key));
            }
            if (categoryId.IsNotEmpty())
            {
                var categoryids = ArticleCategoryService.Instance.
                    GetChildNodeList(categoryId, true).Select(p => p.Id).ToArray();
                query.Where(p => p.CategoryId.In(categoryids));
            }
            query.ExcludeSelect(p => p.Contents);
            return repos.Page(query);
        }
    }
}
